package lib.self.views;

import android.content.Context;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * description: 可以取消滑动的viewpager
 * author: Steven Xiao
 * date: 2017/5/15
 */
public class CustomViewPager extends ViewPager {
    private boolean mScrollable = true;//默认可以滑动
    private boolean mChildrenScrollable = true;//子viewpager是否可以滑动 默认可以

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (mScrollable) {
            return super.onInterceptTouchEvent(ev);
        } else {
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mScrollable) {
            return super.onTouchEvent(ev);
        } else {
            return false;
        }
    }

    public boolean isScrollable() {
        return mScrollable;
    }

    public void setScrollable(boolean scrollable) {
        this.mScrollable = scrollable;
    }

    public boolean isChildrenScrollable() {
        return mChildrenScrollable;
    }

    public void setChildrenScrollable(boolean childrenScrollable) {
        this.mChildrenScrollable = childrenScrollable;
    }

    /**
     * 解决viewpager嵌套viewpager 滑动冲突的问题
     * 如果内部的viewpager需要滑动 return true
     * 内部的viewpager不需要滑动 return false
     */
    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        if (v != this && v instanceof ViewPager) {
            return mChildrenScrollable;
        }
        return super.canScroll(v, checkV, dx, x, y);
    }
}
